package com.dev.springBootDemo.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author zhumq
 * @date 2024/7/2 22:59
 */
public class MybatisTest {
    public static void main(String[] args) throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis配置文件路径");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        // Mapper mapper = sqlSession.getMapper(Mapper类.class);
        // mapper.方法名();
    }

    // 二级缓存
    public void test() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis配置文件路径");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
        SqlSession sqlSession1 = sqlSessionFactory.openSession(true);
        SqlSession sqlSession2 = sqlSessionFactory.openSession(true);
        // Mapper类.class mapper1 = sqlSession1.getMapper(Mapper类.class);
        // Mapper类.class mapper2 = sqlSession2.getMapper(Mapper类.class);
        // mapper1.方法名();
        // sqlSession1.close();// 关闭session，保存在二级缓存中
        // mapper2.方法名();
        // sqlSession2.close();
    }
}
